package com.jackrain.clouddata.sys.sender;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.web.query.QueryEngine;

import java.io.IOException;
import java.util.UUID;

/**
 * Created by Lucky dog on 2017/8/19.
 */
public class BackSender {

    public static void send(String topic,String tag,String ad_client_id,String billNO) {
        ProducerBean producerBean = (ProducerBean) ApplicationContextHandle.getBean(ProducerBean.class);
//        Object omsSql = null;
//        Object omsTime = null;
        //获取SQL和TIME

        String configSql = "SELECT a.SQL_VALUE FROM OMS_SQL a,OMS_TOPIC b\n" +
                "WHERE a.AD_CLIENT_ID = b.AD_CLIENT_ID\n" +
                "AND a.TOPICID = b.ID\n" +
                "AND b.TOPIC=?";

        JSONObject config = QueryEngine.getInstance().doQueryObject(configSql,new Object[]{topic});

        if (config == null) {
            throw new NDSException("SQL语句或TIME未找到");
        }
//        JSONObject omsSqlJson = JSON.parseObject(omsSql.toString());
//        JSONObject omsTimeJson = JSON.parseObject(omsTime.toString());

        JSONObject sqlJson = config.getJSONObject("SQL_VALUE");

        //获取SQL
        String sql = sqlJson.getString("SQL");
        //获取NAME
        String name = sqlJson.getString("NAME");
        //获取ITEM
        JSONArray itemArray = sqlJson.getJSONArray("ITEM");

        JSONObject jo = new JSONObject();
        JSONArray  jaItem = new JSONArray();
        JSONObject mainObject = QueryEngine.getInstance().doQueryObject(sql, new Object[]{billNO,ad_client_id});
        jo.put("name", name);
        jo.put("value",mainObject);

        if (mainObject != null) {
            for (int i = 0; i < itemArray.size(); i++) {
                JSONObject joItem = new JSONObject();
                JSONObject itemConfig = itemArray.getJSONObject(i);
                String itemSql = itemConfig.getString("SQL");
                String itemName = itemConfig.getString("NAME");
                JSONArray item = QueryEngine.getInstance().doQueryObjectArray(itemSql, new Object[]{billNO,ad_client_id});
                joItem.put("name",itemName);
                joItem.put("value",item);
                jaItem.add(joItem);
            }
            jo.put("item",jaItem);
            JSONObject result = new JSONObject();
            result.put("classname","com.jackrain.nea.oms.listener.BackMessageProcess");
            result.put("data",jo);

            try {
                Message msg = new Message("OMS_M_BACK",tag+":"+topic, MsgConvertUtil.objectSerialize(JSON.toJSONString(result,new SerializerFeature[]{SerializerFeature.WriteMapNullValue})));
                msg.setKey(UUID.randomUUID().toString());
                producerBean.send(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
